package{
public class StringU{ //string utilities
	//defines additional function for string
	//extending the String class is not possible, because it's final (see http://www.kirupa.com/forum/showthread.php?333993-Extending-the-String-class-possible)
	//so I made this
	public static function replace(str:String,find:String,rep:String):String{
		return str.split(find).join(rep);
	}
	public static function findClosing(str:String, startI:Number, st:String, en:String):Number{
		//st and en for example : { }
		var stCount:Number = 0;
		for(var i=startI;i<str.length;i++){
			var c:String = str.charAt(i);
			if(c == st){
				//if there's another {
				stCount++;
			}
			if(stCount == 0 && c == en){
				//the correct closing bracket is :
				return i;
			}
			if(stCount > 0 && c == en){
				stCount--;
			}
		}
		return -1; //not found
	}

	public static function trim(s:String):String{
		//removes beginning and trailing spaces
		return s.replace(/^\s+|\s+$/gs, '');
	}
	public static function splitT(str:String,delim:String):Array{
		//split and then trim the results. Deletes empty element
		var res:Array = str.split(delim);
		for(var i=0;i<res.length;i++){
			res[i] = trim(res[i]);
			if(res[i] == ""){
				res.splice(i,1);
				i--;
			}
		}
		return res;
	}
	public static function count(str:String, ch:String):Number{
		return str.split(ch).length - 1;
	}
	public static function getData(obj:Object){
		//converts to number if the data is a number, otherwise string
		obj = replace(obj,"\"","");
		if(!isNaN(obj) && new String(obj).length > 0){
			return new Number(obj).valueOf();
		}else{
			return fixStr(new String(obj).valueOf());
		}
	}
	public static function fixStr(str:String):String{
		//fixes \n not displayed correctly if it's coming from text file
		return str.split("\\n").join("\n");
	}
	public static function setCharAt(str:String,index:Number,char:String):String{
		return str.substr(0,index) + char + str.substr(index + 1);
	}
}
}